﻿using System;
using System.Collections.Generic;
using MockDataTransferObject;
using MockCommon;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;

namespace MockDataAccess
{

    public class TimesheetStatusDataAccess : BaseDataAccess<TimesheetStatus>
    {
        private Collection<SqlParameter> _parameters;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="TimesheetStatusDataAccess"/> class.
        /// </summary>
        public TimesheetStatusDataAccess()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TimesheetStatusDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public TimesheetStatusDataAccess(bool isFake)
            : base(isFake)
        {
        }
        #region Is
        #endregion

        #region Get
        /// <summary>
        /// Retrieves all timesheet statuses.
        /// </summary>
        /// <returns></returns>
        public DataTable RetrieveAllTimesheetStatuses()
        {
            DataTable table = ExecuteQueryAsDataTable(Constant.TimesheetStatusGetAll, null);
            return table;
        }

        /// <summary>
        /// Gets the name of the status id by status.
        /// </summary>
        /// <param name="statusName">Name of the status.</param>
        /// <returns></returns>
        public object GetStatusIdByStatusName(string statusName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterStatusName, Value = statusName });
            return ExecuteQueryAsScalar(Constant.TimesheetStatusGetStatusIdByStatusName, _parameters);
        }

        /// <summary>
        /// Gets the name of the by status.
        /// </summary>
        /// <param name="statusName">Name of the status.</param>
        /// <returns></returns>
        public TimesheetStatus GetByStatusName(string statusName)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterStatusName, Value = statusName });
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.TimesheetStatusGetByStatusName, _parameters);
                Collection<AbstractEntity> statuses = ConvertToList(dataTable, typeof(TimesheetStatus));
                if (statuses != null && statuses.Count > 0)
                {
                    return (TimesheetStatus)statuses[0];
                }
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess.GetByStatusName");
            }
            return null;
        }
        #endregion
    }
}
